/**
 * @author: qwesde@hust.edu.cn
 * @date: 2021/2/6 15:31
 */

import {SET_DEFAULT_TERM, SIDEBAR_TOGGLE} from '../EventConstant'
import {getCurrentDate} from "../../utils/date";
import {setDefaultTerm} from "../../api/admin";
import term from "../../views/admin/types/typeList";

const sidebarStatus = "sidebarStatus";

const app = {
    state: {
        sidebar: {
            opened: localStorage.getItem(sidebarStatus) === "1"
        },
        defaultTerm: getCurrentDate()
    },
    mutations: {
        [SIDEBAR_TOGGLE]: state => {
            state.sidebar.opened = !state.sidebar.opened;
            localStorage.setItem(sidebarStatus, state.sidebar.opened ? "1" : "0");
        },
        [SET_DEFAULT_TERM]: (state, newTerm) => {
            state.defaultTerm = newTerm;
        }
    },
    actions: {
        SidebarToggle: ({commit}) => {
            commit(SIDEBAR_TOGGLE);
        },
        UpdateDefaultTerm({commit}, newTerm) {
            return new Promise(resolve => {
                setDefaultTerm(newTerm).then(res => {
                    commit(SET_DEFAULT_TERM, newTerm);
                    resolve(res);
                })
            })
        }
    }
}

export default app;